//****************************************************************
//* Copyright (c) 2004 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   UsageDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Apr 13 2009 10:13:38  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas104SfiVw;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp07Usage;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class UsageDao extends Dao {

    private static final String CLASS_NAME = UsageDao.class.getName();

    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);
    
    /**
	 * Busco todas las piezas y sus usos de la tabla usage por SFI
	 * 
	 * @return
	 * @throws PersistenceException
	 */
	public Collection<Teesp07Usage> findUsageBySFIDate(
			Timestamp fechaInicio, Timestamp fechaFin, Teesg01Plant teesg01Plant, Ncas104SfiVw sfi) throws PersistenceException {

		String METHOD_NAME = "findUsageBySFIDate";
		log.entering(CLASS_NAME, METHOD_NAME);

		Collection<Teesp07Usage> resultCol = null;
		PersistenceManager pm = null;

		pm = PersistenceUtil.getPersistenceManager();
		Query query = pm.newQuery("findUsageBySFIDate",
				Teesp07Usage.class);

		Object[] parametros = { fechaInicio, fechaFin, teesg01Plant, sfi };

		query.setParameterArray(parametros);

		resultCol = (Collection) pm.executeQuery(query);

		log.exiting(CLASS_NAME, METHOD_NAME);
		return resultCol;
	}
    /**
     * Crea un usage segun el dato proporcionado
     * @param usage
     * @throws PersistenceException
     */
    public void createUsage(Teesp07Usage usage) throws PersistenceException {
        String METHOD_NAME = "createSupplier";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(usage);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Obtiene el usage para actulizar
     * 
     * @param usage
     * @return
     */
    public Teesp07Usage getUsageByUpdate(Teesp07Usage usage)  throws FullEconomicsException {

        String METHOD_NAME = "findCurrencyByCode";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesp07Usage result = new Teesp07Usage();
        
        try {
        	result = (Teesp07Usage) this.getObjForUpdate(usage);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Find Currency By Code Exception", e);
            throw new FullEconomicsException(e,FullEconomicsPrs.MessageErrors.Currencys.FINDBYCODE);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);

        return result;
    }
    
    public void deleteUsage(Teesp07Usage usage) throws PersistenceException {
        String METHOD_NAME = "deleteUsage";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(usage);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }
}
